package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.DepartMentDao;
import com.hrbxlh.util.pageUtil;

@Component("departMentActionImpl")
@Transactional
public class DepartMentActionImpl {
	
	@Resource(name = "departMentDao")
	private DepartMentDao departMentDao;
	
	/**
	 * @description 获取全部院系信息
	 * @param Map<String, Object> 如果有查询条件，就将条件set到map中，
	 *        如果是数据库存在的字段，key值要与column的值相同。
	 * @return List<Map<String,Object>> (院系集合)
	 * @throws Exception
	 */
	public List<Map<String, Object>> getDepartMentList(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList=departMentDao.getDepartMentList(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return returnList;
	}
	 
	 /**
	 * @description 查询院系列表  分页
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getDepartmentList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> homeworkList = departMentDao.getDepartmentList(paramMap);
			resultMap.put("departmentList", homeworkList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = departMentDao.getDepartmentList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	 /**
	 * @description 查询全部院系
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public List<Map<String,Object>> getDepartmentAll(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		try{
			resultList = departMentDao.getDepartmentList(paramMap);
		}catch(Exception e){
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 添加院系
	 * @param Map<String,Object> paramMap  院系名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> addDepartment(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
			List<Map<String, Object>> departmentList = departMentDao.getDepartmentByName(paramMap);
			//判断院系名重复
			if(departmentList != null && departmentList.size() != 0){
				failMsg = "该院系名已存在";
			}else{
				//添加院系
				departMentDao.addDepartment(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 修改院系
	 * @param Map<String,Object> paramMap  院系名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveDepartment(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
			List<Map<String, Object>> departmentList = departMentDao.getDepartmentByName(paramMap);
			//判断院系名重复
			if(departmentList != null && departmentList.size() != 0){
				failMsg = "该院系名已存在";
			}else{
				//修改院系
				departMentDao.saveDepartment(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 删除院系
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String, Object> delDepartment(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String[] tables = {"profession","relation"};	
			String[] errMsg = {"专业","课程"};	
			Map<String, Object> resultMap = this.getDepartmentDelMark("院系",tables, errMsg, "departmentId", (Integer)paramMap.get("departmentId"));	
			boolean resultMark = (Boolean)resultMap.get("result");
			if(resultMark){
				//删除院系信息
				departMentDao.delDepartment(paramMap);
				result.put("result", true);
			}else{
				result.put("result", false);
				result.put("failMsg", resultMap.get("failMsg"));
			}
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 查询院系的专业列表  分页
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getDepartmentProfesList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> homeworkList = departMentDao.getDepartmentProfesList(paramMap);
			resultMap.put("professionList", homeworkList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = departMentDao.getDepartmentProfesList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询某个院系是否能删除的标识（也可用于其他数据，需要将数据库表名传进来，还有要判断的字段）
	 * @param String[] tables(要查询的表),String[] errMsg(每个表都是什么表), String column（要查询的字段）,String columnValues（字段值）
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getDepartmentDelMark(String useName,String[] tables,String[] errMsg, String column,int columnValues) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			resultMap.put("result", true);
			Map<String, Object> paramMap ;
			List<Map<String, Object>> list;
			for(int i = 0; i < tables.length; i++){
				paramMap = new HashMap<String, Object>();
				paramMap.put("tableName", tables[i]);
				paramMap.put("column", column);
				paramMap.put("columnValues", columnValues);
				paramMap.put("delMark", 0);
				list = departMentDao.getMsgList(paramMap);
				if(list != null && list.size() != 0){
					resultMap.put("result", false);
					resultMap.put("errorTitle", tables[i]);
					//resultMap.put("failMsg", errMsg[i]+"还有关联信息不能删除");
					resultMap.put("failMsg", "该"+useName+"下存在"+errMsg[i]+"信息该记录无法删除");
					break;
				}
			}
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	 /**
	 * @description 通过主键查询某院系信息
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getDepartmentDetail(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			resultMap = departMentDao.getDepartmentDetail(paramMap);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	 /**
	 * @description 通过某院系id查询下面全部专业信息
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public List<Map<String,Object>> getMyProfessionList(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		try{
			resultList = departMentDao.getMyProfessionList(paramMap);
		}catch(Exception e){
			throw e;
		}
		return resultList;
	}
}
